//
//  ZXZMyViewController.m
//  zuxianzhi
//
//  Created by tyu on 27.5.24.
//

#import "ZXZMyViewController.h"
#import "ZXZMyCell.h"
#import "ZXZHomeGoodsViewController.h"
#import "ZXZMyTopView.h"
#import "ZXZMyAboutViewController.h"
#import "ZXZMyOrderViewController.h"
#import "ZXZMyCustomViewController.h"

@interface ZXZMyViewController ()<UIScrollViewDelegate>

@property(nonatomic,weak) UIScrollView *ScrollView;
@property(nonatomic,weak) ZXZMyCell *goodsView1;
@property(nonatomic,weak) ZXZMyCell *goodsView2;
@property(nonatomic,weak) ZXZMyCell *goodsView3;
@property(nonatomic,weak) ZXZMyCell *goodsView4;
@property(nonatomic,weak) ZXZMyCell *goodsView5;
@property(nonatomic,weak) ZXZMyTopView *leftView;
@property(nonatomic,weak) ZXZMyTopView *rightView;
@property(nonatomic,weak) UILabel *userNameLab;

@end

@implementation ZXZMyViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
    if (self.navigationController.childViewControllers.count == 1) {
        self.tabBarController.tabBar.hidden = NO;
    }
    [self.navigationController setNavigationBarHidden:YES];
    
    if ([[userDef valueForKey:islogin] intValue] != 1) {
        self.userNameLab.text = @"未登录";
    }else{
        self.userNameLab.text = [userDef valueForKey:userName];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIView ColorWithHexString:@"#F8F9FB" alpha:1];
}

- (void)bac_addUI {
    [super bac_addUI];

    self.navView.hidden = YES;
    self.bacbBackBtn.hidden = YES;
    [self.view bringSubviewToFront:self.navView];
    
    [self setupView];
}

- (void)setupView{
    UIScrollView *ScrollV = [[UIScrollView alloc]init];
    [self.view addSubview:ScrollV];
    self.ScrollView =ScrollV;
    ScrollV.delegate = self;

    ScrollV.contentSize = CGSizeMake(ScreenWidth, ScreenHeight*1.5);
    ScrollV.contentInset = UIEdgeInsetsMake(0, 0, -10, 0);
    ScrollV.showsVerticalScrollIndicator = NO;
    ScrollV.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-ShowDiff);
    
    UIImageView *topImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"touxiang"]];
    [ScrollV addSubview:topImageV];

    topImageV.userInteractionEnabled = YES;
    topImageV.frame = CGRectMake(ScreenWidth/2-50, 100, 100, 100);
    
    topImageV.userInteractionEnabled = YES;
    UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topViewImageClick)];
    [topImageV addGestureRecognizer:Tap];
    
    UILabel *titleLeftLab = [[UILabel alloc]init];
    [self.ScrollView addSubview:titleLeftLab];
    self.userNameLab = titleLeftLab;
    titleLeftLab.text = [userDef valueForKey:userName];
    titleLeftLab.textColor = [UIColor blackColor];
    titleLeftLab.font = [UIFont systemFontOfSize:24];
    titleLeftLab.frame = CGRectMake(0, CGRectGetMaxY(topImageV.frame)+10, ScreenWidth, 40);
    titleLeftLab.textAlignment = NSTextAlignmentCenter;
    
    ZXZMyTopView *leftView = [[ZXZMyTopView alloc]init];
    [self.ScrollView addSubview:leftView];
    leftView.TitleLab.text = @"我的订单";
    leftView.leftImageV.image = [UIImage imageNamed:@"my_leftView"];
    self.leftView = leftView;
    leftView.ClcikBlock = ^(NSString * _Nonnull Str) {
        [self checkIsLogin:^{
            ZXZMyOrderViewController *vc = [ZXZMyOrderViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    };
    
    leftView.layer.cornerRadius = 15;
    leftView.layer.masksToBounds = YES;
    
    ZXZMyTopView *rightView = [[ZXZMyTopView alloc]init];
    [self.ScrollView addSubview:rightView];
    rightView.TitleLab.text = @"我的定制";
    rightView.leftImageV.image = [UIImage imageNamed:@"my_rightView"];
    self.rightView = rightView;
    rightView.ClcikBlock = ^(NSString * _Nonnull Str) {
        [self checkIsLogin:^{
            ZXZMyCustomViewController *vc = [ZXZMyCustomViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    };
    
    rightView.layer.cornerRadius = 15;
    rightView.layer.masksToBounds = YES;
    CGFloat leftViewW = (ScreenWidth-30-15)/2;
    leftView.frame = CGRectMake(15, CGRectGetMaxY(titleLeftLab.frame)+20, leftViewW, 60);
    rightView.frame = CGRectMake(CGRectGetMaxX(leftView.frame)+15, CGRectGetMinY(leftView.frame), leftViewW, 60);

    for (int i=0; i<5; i++) {
        ZXZMyCell *goods = [[ZXZMyCell alloc]init];
        [self.ScrollView addSubview:goods];
        if (i==0) {
            goods.TitleLab.text = @"客服咨询";
            self.goodsView1 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                [self showKefuAlertView];
            };
        }else if (i==1){
            goods.TitleLab.text = @"关于我们";
            self.goodsView2 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                
                ZXZMyAboutViewController *vc = [ZXZMyAboutViewController new];
                [self.navigationController pushViewController:vc animated:YES];
                
            };
        }else  if (i==2){
            goods.TitleLab.text = @"清除缓存";
            self.goodsView3 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                [NotifyLabel NotifyWithString:@"清除缓存成功"];
                
            };
        }else  if (i==3){
            goods.TitleLab.text = @"退出登录";
            self.goodsView4 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                [self checkIsLogin:^{
                    [self showAlertWithMessage:@"您确定要退出登录吗?" sureAction:^{
                        [userDef setValue:@"0" forKey:islogin];
                        [self.navigationController pushViewController:[ZXZLoginViewController new] animated:YES];
                    }];
                }];
            };
            
        }else  if (i==4){
            goods.TitleLab.text = @"注销账号";
            self.goodsView5 = goods;
            goods.ClcikBlock = ^(NSString * _Nonnull Str) {
                
                [self checkIsLogin:^{
                    
                    [self showAlertWithMessage:@"您确定要注销账号吗?" sureAction:^{
                        [userDef setValue:@"0" forKey:islogin];
                        [self.navigationController pushViewController:[ZXZLoginViewController new] animated:YES];
                    }];
                }];
            };
        }
    }
    
    
    CGFloat height = 60;
    CGFloat width = (ScreenWidth-30);

    self.goodsView1.frame = CGRectMake(15, CGRectGetMaxY(leftView.frame)+20, width, height);
    
    self.goodsView1.layer.cornerRadius = 10;
    self.goodsView1.layer.masksToBounds = YES;
    [self.goodsView1.layer setMaskedCorners:kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner];

    self.goodsView2.frame = CGRectMake(15, CGRectGetMaxY(self.goodsView1.frame), width, height);
    self.goodsView3.frame = CGRectMake(15, CGRectGetMaxY(self.goodsView2.frame), width, height);
    
    self.goodsView3.layer.cornerRadius = 10;
    self.goodsView3.layer.masksToBounds = YES;
    [self.goodsView3.layer setMaskedCorners:kCALayerMaxXMaxYCorner | kCALayerMinXMaxYCorner];


    self.goodsView4.frame = CGRectMake(15, CGRectGetMaxY(self.goodsView3.frame)+10, width, height);
    self.goodsView4.layer.cornerRadius = 10;
    self.goodsView4.layer.masksToBounds = YES;
    [self.goodsView4.layer setMaskedCorners:kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner];

    

    self.goodsView5.frame = CGRectMake(15, CGRectGetMaxY(self.goodsView4.frame), width, height);
    self.goodsView5.layer.cornerRadius = 10;
    self.goodsView5.layer.masksToBounds = YES;
    [self.goodsView5.layer setMaskedCorners:kCALayerMaxXMaxYCorner | kCALayerMinXMaxYCorner];

}


- (void)topViewImageClick
{
    [self checkIsLogin:^{
        
    }];
}

- (void)showKefuAlertView
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:defServerPhone preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)checkIsLogin:(void(^)(void))complete
{
    if ([[userDef valueForKey:islogin] intValue] != 1) {
        [self.navigationController pushViewController:[ZXZLoginViewController new] animated:YES];
        return;
    }else{
        if (complete){
            complete();
        }
    }

}



- (void)showAlertWithMessage:(NSString *)message
                  sureAction:(void(^)(void))sureAction
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (sureAction)
        {
            sureAction();
        }
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:doneAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
